package com.hixtrip.sample.app.service;

import com.hixtrip.sample.app.api.PayResultService;
import com.hixtrip.sample.client.constant.PayStatus;
import com.hixtrip.sample.client.order.dto.CommandPayDTO;
import com.hixtrip.sample.client.sample.vo.PayCallbackVO;
import com.hixtrip.sample.domain.order.OrderDomainService;
import com.hixtrip.sample.domain.pay.PayDomainService;
import com.hixtrip.sample.domain.pay.model.CommandPay;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 支付成功
 */
@Service(PayResultService.BEAN_NAME_PREFIX + PayStatus.PAY_SUCCEED_STATUS)
public class PayResultSucceedService implements PayResultService {

    @Autowired
    private PayDomainService payDomainService;

    @Autowired
    private OrderDomainService orderDomainService;

    /**
     * 支付成功处理
     */
    @Override
    public PayCallbackVO dealPayResult(CommandPayDTO commandPayDTO) {
        // TODO
        CommandPay commandPay = new CommandPay();
        BeanUtils.copyProperties(commandPayDTO, commandPay);

        // 支付成功后修改订单
        orderDomainService.orderPaySuccess(commandPay);

        // 记录支付结果
        payDomainService.payRecord(commandPay);

        return PayCallbackVO.builder().id(commandPay.getOrderId())
                .code("200")
                .msg("操作成功")
                .build();
    }
}
